package vip.raod.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import vip.raod.common.domain.Tree;
import vip.raod.common.utils.BuildTree;
import vip.raod.system.dao.DeptDao;
import vip.raod.system.domain.DeptDO;
import vip.raod.system.service.DeptService;



@Service
public class DeptServiceImpl implements DeptService {
	@Autowired
	private DeptDao sysDeptMapper;

	@Override
	public DeptDO get(Long deptId){
		return sysDeptMapper.get(deptId);
	}

	@Override
	public List<DeptDO> list(Map<String, Object> map){
		return sysDeptMapper.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return sysDeptMapper.count(map);
	}

	@Override
	public int save(DeptDO sysDept){
		return sysDeptMapper.save(sysDept);
	}

	@Override
	public int update(DeptDO sysDept){
		return sysDeptMapper.update(sysDept);
	}

	@Override
	public int remove(Long deptId){
		return sysDeptMapper.remove(deptId);
	}

	@Override
	public int batchRemove(Long[] deptIds){
		return sysDeptMapper.batchRemove(deptIds);
	}

	@Override
	public Tree<DeptDO> getTree(Long deptId, Long userId) {
		List<Tree<DeptDO>> trees = new ArrayList<Tree<DeptDO>>();

		HashMap<String, Object> stringObjectHashMap = new HashMap<>(16);
        List<DeptDO> sysDepts = null;
        if (userId != 1L && userId != 2L) {
            deptList.clear();
			Long parentDept = getParentDept(deptId);
			getSonDept(parentDept);
            DeptDO deptDO = get(parentDept);
            deptList.add(deptDO);
            sysDepts = deptList;
        } else {
            sysDepts = sysDeptMapper.list(new HashMap<>(16));
        }

        for (DeptDO sysDept : sysDepts) {
			Tree<DeptDO> tree = new Tree<DeptDO>();
			tree.setId(sysDept.getDeptId().toString());
			tree.setParentId(sysDept.getParentId().toString());
			tree.setText(sysDept.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<DeptDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	public boolean checkDeptHasUser(Long deptId) {
		// TODO Auto-generated method stub
		//查询部门以及此部门的下级部门
		int result = sysDeptMapper.getDeptUserNumber(deptId);
		return result==0?true:false;
	}

    List<DeptDO> deptList = new ArrayList<>();
    public void getSonDept(Long deptId) {
		Map<String, Object> state = new HashMap<>(16);
		state.put("parentId", deptId);
        List<DeptDO> dList = list(state);
        for (DeptDO deptDO : dList) {
            deptList.add(deptDO);
            getSonDept(deptDO.getDeptId());
        }
        if (dList.size() == 0) {
            return;
        }
    }

	public Long getParentDept(Long deptId) {
		DeptDO deptDO = get(deptId);
        if (deptDO.getParentId() == 0) {
            return deptDO.getDeptId();
        }
       return getParentDept(deptDO.getParentId());
	}
}
